package com.example.composableexample

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import coil.compose.rememberImagePainter
import com.example.composableexample.systembar.SystemBarDetail
import com.example.composableexample.systembar.SystemBarSimple
import com.example.composableexample.ui.theme.ComposableExampleTheme
import com.example.composableexample.ui.QrCodeScreen
import com.example.composableexample.util.rememberRandomSampleImageUrl
import com.example.composableexample.util.supportWideScreen
import com.google.accompanist.systemuicontroller.SystemUiController
import com.google.accompanist.systemuicontroller.rememberSystemUiController

class SystemUiActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposableExampleTheme {
                QrCodeScreen()
            }
        }
    }
}

@Composable
fun WelcomeScreen() {

    var showBranding by remember { mutableStateOf(true) }

    Surface(modifier = Modifier.supportWideScreen()) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .verticalScroll(rememberScrollState())
        ) {
            Spacer(
                modifier = Modifier
                    .weight(1f, fill = showBranding)
                    .animateContentSize()
            )

            AnimatedVisibility(
                showBranding,
                Modifier.fillMaxWidth()
            ) {
                Branding()
            }
        }
    }
}

@Composable
fun Branding(modifier: Modifier = Modifier) {
    Column(
        modifier = modifier.wrapContentHeight(align = Alignment.CenterVertically)
    ) {
        Logo(
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
                .padding(horizontal = 76.dp)
        )
        Text(
            text = stringResource(id = R.string.app_tagline),
            style = MaterialTheme.typography.titleMedium,
            textAlign = TextAlign.Center,
            modifier = Modifier
                .padding(top = 24.dp)
                .fillMaxWidth()
        )
    }
}

@Composable
private fun Logo(
    modifier: Modifier = Modifier,
    lightTheme: Boolean = LocalContentColor.current.luminance() < 0.5f,
) {
    val assetId = if (lightTheme) {
        R.drawable.ic_logo_light
    } else {
        R.drawable.ic_logo_dark
    }
    Image(
        painter = painterResource(id = assetId),
        modifier = modifier,
        contentDescription = null
    )
}

@Preview(name = "Welcome light theme", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(name = "Welcome dark theme", uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
fun WelcomeScreenPreview() {
    ComposableExampleTheme {
        WelcomeScreen()
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun Sample(systemUiController: SystemUiController) {

    // Get the current SystemUiController
//    val systemUiController = rememberSystemUiController()
    var clickedColor by remember { mutableStateOf(Color.Unspecified) }
    var statusBarDarkIcons by remember { mutableStateOf(false) }
    var navigationBarDarkIcons by remember { mutableStateOf(false) }


    // Update the light and dark status bar state for both the Activity's SystemUiController and
    // the Dialog's. If we just updated the Dialog's things wouldn't work properly for unknown
    // reasons.
    LaunchedEffect(systemUiController, statusBarDarkIcons, navigationBarDarkIcons) {
//        systemUiController.statusBarDarkContentEnabled = statusBarDarkIcons
//        systemUiController.navigationBarDarkContentEnabled = navigationBarDarkIcons
//        parentSystemUiController.statusBarDarkContentEnabled = statusBarDarkIcons
//        parentSystemUiController.navigationBarDarkContentEnabled = navigationBarDarkIcons
    }

    @Composable
    fun Color(color: Color) {
        Box(
            modifier = Modifier
                .size(54.dp)
                .clickable { clickedColor = color },
            contentAlignment = Alignment.Center
        ) {
            if (clickedColor == color) {
                Box(
                    Modifier
                        .size(48.dp)
                        .background(Color.Black.copy(alpha = 0.6f))
                )
            }
            Box(modifier = Modifier.size(44.dp)) {
                Image(
                    painter = painterResource(R.drawable.jetpack),
                    contentDescription = null,
                    contentScale = ContentScale.FillBounds,
                    modifier = Modifier.fillMaxSize()
                )
                Box(
                    Modifier
                        .fillMaxSize()
                        .background(color)
                )
            }
        }
    }

    BoxWithConstraints(Modifier.fillMaxSize()) {
        // Displaying a random image
        Image(
            painter = rememberImagePainter(
                data = with(LocalDensity.current) {
                    rememberRandomSampleImageUrl(
                        seed = 16,
                        width = maxWidth.roundToPx(),
                        height = maxHeight.roundToPx()
                    )
                },
            ),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.fillMaxSize()
        )
        Column(
            Modifier
                .fillMaxSize()
                .safeDrawingPadding()
        ) {
            LargeTopAppBar(
                title = { Text(stringResource(R.string.system_ui_controller_title_color_dialog)) },
                colors = TopAppBarDefaults.largeTopAppBarColors()
            )
            Spacer(modifier = Modifier.weight(1f))
            var text by remember { mutableStateOf(TextFieldValue("Test for IME support")) }
            TextField(value = text, onValueChange = { text = it })
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .fillMaxWidth()
                    .verticalScroll(rememberScrollState())
                    .background(MaterialTheme.colorScheme.surface.copy(alpha = 0.5f))
                    .padding(vertical = 16.dp),
            ) {
                Row {
                    Color(Color.Black)
                    Color(Color.DarkGray)
                    Color(Color.Gray)
                    Color(Color.LightGray)
                }
                Row {
                    Color(Color.White)
                    Color(Color.Red)
                    Color(Color.Green)
                    Color(Color.Blue)
                }
                Row {
                    Color(Color.Yellow)
                    Color(Color.Cyan)
                    Color(Color.Magenta)
                    Color(Color(0xFF673AB7))
                }
                Row {
                    Color(Color.Black.copy(alpha = 0.5f))
                    Color(Color.Blue.copy(alpha = 0.5f))
                    Color(Color.White.copy(alpha = 0.5f))
                    Color(Color.Transparent)
                }
                Spacer(modifier = Modifier.height(16.dp))
                Button(
                    onClick = {
                        systemUiController.setStatusBarColor(clickedColor)
                        statusBarDarkIcons = clickedColor.luminance() > 0.5f
                    },
                    modifier = Modifier
                        .fillMaxWidth(0.7f)
                        .padding(vertical = 8.dp)
                ) {
                    Text(text = "Change status bar color")
                }
                Button(
                    onClick = {
                        systemUiController.setNavigationBarColor(clickedColor)
                        navigationBarDarkIcons = clickedColor.luminance() > 0.5f
                    },
                    modifier = Modifier
                        .fillMaxWidth(0.7f)
                        .padding(vertical = 8.dp)
                ) {
                    Text(text = "Change navigation bar color")
                }
                Button(
                    onClick = {
                        systemUiController.setSystemBarsColor(clickedColor)
                        statusBarDarkIcons = clickedColor.luminance() > 0.5f
                        navigationBarDarkIcons = statusBarDarkIcons
                    },
                    modifier = Modifier
                        .fillMaxWidth(0.7f)
                        .padding(vertical = 8.dp)
                ) {
                    Text(text = "Change system bars color")
                }

                Row(
                    modifier = Modifier
                        .clickable {
                            statusBarDarkIcons = !statusBarDarkIcons
                        }
                        .padding(8.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(text = "Set status bar dark icons  ")
                    Checkbox(
                        checked = statusBarDarkIcons,
                        onCheckedChange = {
                            statusBarDarkIcons = it
                        }
                    )
                }
                Row(
                    modifier = Modifier
                        .clickable {
                            navigationBarDarkIcons = !navigationBarDarkIcons
                        }
                        .padding(8.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(text = "Set navigation bar dark icons  ")
                    Checkbox(
                        checked = navigationBarDarkIcons,
                        onCheckedChange = {
                            navigationBarDarkIcons = it
                        }
                    )
                }
            }
        }
    }
}
